package com.ruoyi.project.gxgw_modules.adsense.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.gxgw_modules.adsense.domain.Adsense;
import com.ruoyi.project.gxgw_modules.adsense.service.IAdsenseService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;

/**
 * 广告位管理 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-01-18
 */
@Controller
@RequestMapping("/gxgw_modules/adsense")
public class AdsenseController extends BaseController {
	private String prefix = "gxgw_modules/adsense";

	@Autowired
	private IAdsenseService adsenseService;

	@RequiresPermissions("gxgw_modules:adsense:view")
	@GetMapping()
	public String adsense() {
		return prefix + "/adsense";
	}

	/**
	 * 查询广告位管理列表
	 */
	@RequiresPermissions("gxgw_modules:adsense:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Adsense adsense) {
		startPage();
		List<Adsense> list = adsenseService.selectAdsenseList(adsense);
		return getDataTable(list);
	}

	/**
	 * 导出广告位管理列表
	 */
	@RequiresPermissions("gxgw_modules:adsense:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Adsense adsense) {
		List<Adsense> list = adsenseService.selectAdsenseList(adsense);
		ExcelUtil<Adsense> util = new ExcelUtil<Adsense>(Adsense.class);
		return util.exportExcel(list, "adsense");
	}

	/**
	 * 新增广告位管理
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存广告位管理
	 */
	@RequiresPermissions("gxgw_modules:adsense:add")
	@Log(title = "广告位管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Adsense adsense) {
		Date date = new Date();
		Timestamp timeStamp = new Timestamp(date.getTime());
		adsense.setCreateTime(timeStamp);

		adsense.setCreateBy(ShiroUtils.getSysUser().getUserName());
		return toAjax(adsenseService.insertAdsense(adsense));
	}

	/**
	 * 修改广告位管理
	 */
	@GetMapping("/edit/{adsId}")
	public String edit(@PathVariable("adsId") Integer adsId, ModelMap mmap) {
		Adsense adsense = adsenseService.selectAdsenseById(adsId);
		mmap.put("adsense", adsense);
		return prefix + "/edit";
	}

	/**
	 * 修改保存广告位管理
	 */
	@RequiresPermissions("gxgw_modules:adsense:edit")
	@Log(title = "广告位管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Adsense adsense) {
		Date date = new Date();
		// 设置修改日期
		Timestamp timeStamp = new Timestamp(date.getTime());
		adsense.setUpdateTime(timeStamp);
		// 设置修改人
		adsense.setUpdateBy(ShiroUtils.getSysUser().getUserName());
		return toAjax(adsenseService.updateAdsense(adsense));
	}

	/**
	 * 删除广告位管理
	 */
	@RequiresPermissions("gxgw_modules:adsense:remove")
	@Log(title = "广告位管理", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		System.out.println(ids);
		// 查询每个id下面否有广告，如果有不允许占用
		String[] id = ids.split(",");
		int flag = 0;
		for (int i = 0; i < id.length; i++) {
			Integer adsId = Integer.parseInt(id[i]);
			if (adsenseService.selectCountAdListById(adsId) > 0) {
				flag = 1;
				return error(1, "所选的广告位已被使用,不允许删除！");
			}
			if (flag == 1) {
				break;
			}
		}
		return toAjax(adsenseService.deleteAdsenseByIds(ids));
	}

}
